rm(list = ls())
library(countrycode)
library(pscl)
library(foreign)
library(readstata13)
library(oc)
library(wnominate)
library(Rarity)

setwd("~/Desktop/QJPS Replication")

#correlation between wnominate, ideal, hierarchical, and oc models
ideal <- read.csv("all_countries_ideal.csv")
ideal$match <- paste(ideal$cown, ideal$year, sep = "-")

wnom <- read.csv("all_countries_WNOMINATE.csv")
wnom$match <- paste(wnom$cowcode, wnom$year, sep = "-")

oc <- read.csv("all_countries_oc.csv")
oc$match <- paste(oc$ccode, oc$year, sep = "-")

hier <- read.csv("all_countries_hierarchical.csv")
hier$match <- paste(hier$ccode, hier$year, sep = "-")

colony <- read.csv("all_countries_hierarchical_colonial.csv")
colony$match <- paste(colony$ccode, colony$year, sep = "-")

ideal$ideal <- -1*ideal$median

m <- match(ideal$match, wnom$match)
table(is.na(m))
ideal$wnom <- wnom$coord1D[m]

m <- match(ideal$match, oc$match)
table(is.na(m))
ideal$oc <- oc$coord1D[m]

m <- match(ideal$match, hier$match)
table(is.na(m))
ideal$hier <- hier$V1[m]*-1

m <- match(ideal$match, colony$match)
table(is.na(m))
ideal$colony <- colony$V1[m]

corPlot(subset(ideal, select = c("ideal", "wnom", "oc", "hier")), pch = 4, xlab = c("Bayesian Rank", "WNOMINATE Rank", "OC Rank", ""), ylab = c("", "WNOMINATE Rank", "OC Rank", "Hierarchical Rank"))



ideal$heir <- -1*(ideal$hier - min(ideal$hier, na.rm = T)) / (max(ideal$hier, na.rm = T) - min(ideal$hier, na.rm = T))

plot(ideal$median, ideal$heir, pch = 16, xlab = "Bayesian Ideal Point Model"
     , ylab = "Hierarchical Ideal Point Model (By Country)", xlim = c(-3.5, 2), 
     ylim = c(-1,0), cex = .5,
     main = "Country Group Model")
cor(ideal$median, ideal$heir, use = "complete.obs")



ideal$colonial <- (ideal$colony - min(ideal$colony, na.rm = T)) / (max(ideal$colony, na.rm = T) - min(ideal$colony, na.rm = T))

plot(ideal$median, ideal$colony, pch = 16, 
     xlab = "Bayesian Ideal Point Model"
     , ylab = "Hierarchical Ideal Point Model (By Colonial Origin)", 
     xlim = c(-3.5, 2), cex = .5,
     main = "Colonial Origin Group Model")
cor(ideal$median, ideal$colonial, use = "complete.obs")
